import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/global.css'
import http from '../http.js'
Vue.use(http)

import axios from 'axios'

// 富文本编辑器
import VueQuillEditor from 'vue-quill-editor'

import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme

Vue.use(VueQuillEditor)


// 设置拦截器
axios.interceptors.request.use(config => {
  // console.log(config);
  NProgress.start();
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // 在最后必须return config
  return config
})

axios.interceptors.request.use(config => {
  NProgress.done();
  return config
})

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')


// 挂载路由导航守卫
router.beforeEach((to,from,next) => {
  // 如果从用户访问的登录页面，直接放行
  if(to.path === '/login_register') return next()
  // 从sessionStorage 中获取保存的token
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有token，强制跳转到登录页面
  if(!tokenStr) return next('/login')
  next()
})
